package com.ptszyxx.popose.module.main.mine;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import com.luck.picture.lib.entity.LocalMedia;
import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ysg.http.data.AppViewModelFactory;
import com.ptszyxx.popose.databinding.FragmentMineBinding;
import com.ysg.http.data.entity.mine.SignResult;
import com.ysg.http.data.entity.mine.VipUserEntity;
import com.ptszyxx.popose.module.main.mine.vm.MineVM;
import com.ysg.base.BaseFragment;
import com.ysg.utils.YCollectionUtil;
import com.ysg.utils.YDialogUtil;
import com.ysg.utils.YStringUtil;
import com.ysg.widget.dialog.AvatarEditDialog;
import com.ysg.widget.dialog.SignDialog;
import com.ysg.widget.pic.YPicUtils;
import java.util.ArrayList;

public class MineFragment extends BaseFragment<FragmentMineBinding, MineVM> {
    public static MineFragment getInstance() {
        return new MineFragment();
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_mine;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public MineVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(MineVM.class);
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.requestUser();
        viewModel.requestVip();
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onVipEvent.observe(this, o -> setVipUI());
        viewModel.uc.onSignEvent.observe(this, o -> showDialogSign((SignResult) o));
        viewModel.uc.onAvatarEditEvent.observe(this, o -> showDialogAvatarEdit());
    }

    /**
     * vip
     */
    private void setVipUI() {
        VipUserEntity user = viewModel.vipResult.getUser();
        if (user == null){
            return;
        }
        String isVip = user.getIsVip();
        if (YStringUtil.eq("1", isVip)) {
            String format = String.format(getResources().getString(R.string.mine_vip_status_open), user.getVipEndDate());
            binding.tvVipStatus.setText(format);
            binding.tvVipStatus.setVisibility(View.VISIBLE);
            binding.tvVipTip.setVisibility(View.GONE);
            binding.tvVipOpen.setText(getResources().getString(R.string.renew));
        } else {
            binding.tvVipStatus.setVisibility(View.GONE);
            binding.tvVipTip.setVisibility(View.VISIBLE);
            binding.tvVipOpen.setText(getResources().getString(R.string.open));
        }
    }

    /**
     * 签到
     */
    private void showDialogSign(SignResult data) {
        YDialogUtil.getInstance().showSign(getContext(), data)
                .setOnSignListener(new SignDialog.OnSignListener() {
                    @Override
                    public void onSwitchClick() {
                        viewModel.requestSignTip();
                    }

                    @Override
                    public void onSignClick() {
                        viewModel.requestSignAdd();
                    }
                });
    }

    /**
     * 上传头像对话框
     */
    private void showDialogAvatarEdit(){
        YDialogUtil.getInstance().showAvatarEdit(getContext()).setOnConfirmListener(new AvatarEditDialog.OnConfirmListener() {
            @Override
            public void onConfirmClick() {
                onSelectAvatar();
            }
        });
    }

    /**
     * 选择头像
     */
    private void onSelectAvatar(){
        YPicUtils.getInstance().addAvatar(getContext(), new YPicUtils.OnResultListener() {
            @Override
            public void onResultListener(ArrayList<LocalMedia> result) {
                if (YCollectionUtil.isNotEmpty(result)){
                    viewModel.requestOosUploadPic(result.get(0).getCutPath());
                }
            }
        });
    }


}
